package com.cskaoyan.wordmemorize.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cskaoyan.wordmemorize.dao.entity.UserCheckStickDO;
import com.cskaoyan.wordmemorize.dao.entity.UserCheckinDO;
import com.cskaoyan.wordmemorize.dao.entity.UserCheckinStatisticsDO;
import com.cskaoyan.wordmemorize.dao.entity.UserCheckinStickDateRangeDO;
import lombok.NonNull;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

public interface UserCheckinMapper extends BaseMapper<UserCheckinDO> {

    //查询是否已经签到
    @Select("SELECT COUNT(*) > 0 FROM user_checkin WHERE user_id = #{userId} AND sign_date = #{signDate}")
    boolean existsByUserIdAndSignDate(@Param("userId") Long userId, @Param("signDate") LocalDate signDate);


    //查询前一天的签到数据
    @Select("SELECT id, user_id, sign_date, type, stick_no, create_time " +
            "FROM user_checkin " +
            "WHERE user_id = #{userId} AND sign_date = #{signDate} AND type = #{type}")
    UserCheckinDO findByUserIdAndSignDateAndType(
            @Param("userId") Long userId,
            @Param("signDate") LocalDate signDate,
            @Param("type") Integer type
    );

    @Select("SELECT COALESCE(MAX(stick_no), 0) FROM user_checkin WHERE user_id = #{userId} AND stick_no > 0")
    Long getMaxStickNo(@Param("userId") Long userId);
}
